<template>
  <div>
    <!-- 名称输入框 -->
    <el-input v-model="searchName"
              placeholder="Search by Name"></el-input>
    <el-button @click="applySearchFilter">Search</el-button>
    <el-button @click="clearSearch">Clear</el-button>
    <el-table :data="pagedData"
              :cell-style="rowBackgroundColor"
              :row-class-name="rowBackgroundColor"
              style="width: 100%">
      <el-table-column v-for="column in dynamicColumns"
                       :key="column.prop"
                       :prop="column.prop"
                       :label="column.label">
        <!-- 自定义列内容 -->
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- 操作按钮 -->
          <el-button type="primary"
                     @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger"
                     @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination :current-page="currentPage"
                   :page-sizes="[10, 20, 50]"
                   :page-size="pageSize"
                   layout="sizes, prev, pager, next, jumper"
                   :total="filteredData.length"
                   @size-change="handleSizeChange"
                   @current-change="handlePageChange">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data () {
    return {
      originalData: [],
      filteredData: [],
      dynamicColumns: [],
      watchedColumns: ["status"],
      searchName: "",
      currentPage: 1,
      pageSize: 10,
    };
  },
  computed: {
    pagedData () {
      const startIndex = (this.currentPage - 1) * this.pageSize;
      const endIndex = startIndex + this.pageSize;
      return this.filteredData.slice(startIndex, endIndex);
    },
  },
  // watch: {
  //   searchName: {
  //     handler: "applySearchFilter",
  //     immediate: true,
  //   },
  // },
  created () {
    this.fetchData();
    // 初始化辅助数据结构
    this.originalData.forEach((row) => {
      row.__prevData = { ...row };
    });
  },
  methods: {
    fetchData () {
      // 模拟从后端获取数据
      const response = [
        { id: 1, name: "Product A", status: "10" },
        { id: 2, name: "Product A11", status: "20" },
        { id: 3, name: "Product A22", status: "10" },
        { id: 4, name: "Product A33", status: "10" },
        { id: 5, name: "Product A44", status: "30" },
        { id: 6, name: "Product A55", status: "40" },
        { id: 7, name: "Product A66", status: "40" },
        { id: 8, name: "Product A77", status: "40" },
        { id: 9, name: "Product A88", status: "40" },
        { id: 10, name: "Product A99", status: "40" },
        { id: 11, name: "Product A111", status: "40" },
        { id: 12, name: "Product A222", status: "40" },
        { id: 13, name: "Product A333", status: "40" },
        { id: 14, name: "Product A444", status: "40" },
        // ...更多数据
      ];

      this.originalData = response;
      this.filteredData = response;
      this.dynamicColumns = [
        { prop: "id", label: "ID" },
        { prop: "name", label: "Name" },
        { prop: "status", label: "Status" },
      ];
    },
    rowBackgroundColor (row) {
      if (this.watchedColumns.some(col => row[col] !== row.__prevData[col])) {
        return 'rowBgColor'
      } else {
        return ''
      }
    },
    handleEdit (row) {
      // 处理编辑操作
      console.log("编辑", row);

      // 将status字段的值在"10"和"20"之间切换
      row.status = row.status === "10" ? "20" : "10";

      // 更新原始数据中对应的行的状态
      const originalRow = this.originalData.find(item => item.id === row.id);
      if (originalRow) {
        originalRow.status = row.status;
      }
    },
    handleDelete (row) {
      // 处理删除操作
      console.log("删除", row);
      // 在这里你可以执行删除操作，并将状态更新到原始数据中
    },
    handleSizeChange (newSize) {
      this.pageSize = newSize;
      this.currentPage = 1;
    },
    handlePageChange (newPage) {
      this.currentPage = newPage;
    },
    applySearchFilter () {
      if (this.searchName.trim() === "") {
        this.filteredData = this.originalData;
      } else {
        this.filteredData = this.originalData.filter((row) => {
          return row.name.toLowerCase().includes(this.searchName.toLowerCase());
        });
      }
      this.currentPage = 1;
    },
    clearSearch () {
      this.searchName = "";
      this.filteredData = this.originalData;
      this.currentPage = 1;
    },
  },
};
</script>

<style lang="scss" scoped>
/deep/.el-table {
  .rowBgColor {
    background-color: rgba(239, 239, 239, 0.9) !important;
  }
}
/* 这里可以定义样式 */
</style>
